# -*- encoding:UTF-8 -*-
# desc: 对安装验证完成的epkg包进行发布，这里发布到pubulish目录
import argparse
import os
import subprocess


def after_del(args):
    """
        后处理
    """
    publish_dir = args.publish_dir
    epkg_name = args.epkg_name
    job_id = os.environ.get("id", "")
    result_root = os.environ.get("result_root", "")
    http_result_host = os.environ.get("SRV_HTTP_RESULT_HOST", "")
    cache_epkg_dir = "/root/.cache/epkg/packages/"

    url = "http://{0}:3080/epkg{1}/".format(http_result_host,
                                       publish_dir)

    command = "curl -sSf -F 'file=@{0}{1}' {2} --cookie JOBID={3}".format(
        cache_epkg_dir,
        epkg_name,
        url,
        job_id
        )
    process = subprocess.Popen(command, shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    stdout, stderr = process.communicate()
    if process.returncode != 0:
        print("ERROR:{0}".format(stderr))
    else:
        print("package {0} pushed the target directory".format(epkg_name))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="处理验证完成的epkg包")
    parser.add_argument("-publish_dir", required=True, default="",
                        help="publish_dir")
    parser.add_argument("-epkg_name", required=True, default="",
                        help="epkg_name")

    args = parser.parse_args()
    after_del(args)
